<template>
  <!-- 一条文章单元格 -->
  <van-cell>
    <!-- 标题区域的插槽 -->
    <template #title>
      <div class="title-box">
        <!-- 标题 -->
        <span>{{ obj.title }}</span>
        <!-- 单图 -->
        <img
          v-if="obj.cover.type === 1"
          :src="obj.cover.images[0]"
          class="thumb"
        />
      </div>
      <!-- 三张图片 -->
      <div class="thumb-box" v-if="obj.cover.type > 1">
        <img
          class="thumb"
          v-for="(imgUrl, index) in obj.cover.images"
          :key="index"
          :src="imgUrl"
        />
      </div>
   </template>
    <!-- label 区域的插槽 -->
    <template #label>
     <div class="label-box">
       <div>
         <span>{{ obj.aut_name }}</span>
         <span>{{ obj.comm_count }}</span>
         <span>{{ obj.pubdate }}</span>
       </div>
      <!-- 反馈按钮 -->
      <van-icon name="cross" @click.stop="show=true" v-if="showX"/>
      <!-- 弹出层 -->
       <van-action-sheet
          v-model="show"
          :actions="actions"
          :cancel-text="cancelText"
          get-container="body"
          @select="onSelect"
          @cancel="onCancel"
        />
     </div>
   </template>
  </van-cell>
</template>

<script>
import { firstActions, secondActions } from '@/api/reports.js'
import { articleDislikesAPI, articleReportsAPI } from '@/api'
import { Notify, Dialog } from 'vant'
//
export default {
  props: {
    obj: {
      type: Object,
      default: () => {}
    },
    showX: {
      type: Boolean,
      default: true // 默认显示
    }
  },
  data () {
    return {
      firstActions,
      secondActions,
      show: false,
      actions: firstActions,
      cancelText: '取消'
    }
  },
  components: {
    [Dialog.Component.name]: Dialog.Component
  },
  methods: {
    // 点击反馈内容
    async onSelect (actions) {
      // console.log(actions)
      // 判断
      if (actions.name === '反馈垃圾内容') {
        this.actions = this.secondActions
        this.cancelText = '返回'
      } else if (actions.name === '不感兴趣') {
        // 不感兴趣
        try {
          await articleDislikesAPI({ target: this.obj.art_id })
          Dialog({ message: '谢谢您的反馈，我们会优化内容，给您最优质的体验' }).then(() => {
            // on confirm
            Notify({ type: 'success', message: '反馈成功' })
          })
          this.show = false
        } catch (error) {
          // Notify({ type: 'warning', message: '反馈失败-联系程序员' })
          // 弹窗
          Dialog.confirm({
            title: '温馨提示',
            message: '反馈失败，请登录'
          }).then(() => {
            // on confirm
            // 跳转到登录页面
            this.$router.push({
              path: '/login'
            })
          })
          this.show = false
        }
      } else {
        // 内容反馈
        try {
          await articleReportsAPI(
            {
              target: this.obj.art_id,
              type: actions.value,
              remark: '就是要投诉'
            }
          )
          // Notify({ type: 'success', message: '反馈成功' })
          Dialog({ message: '谢谢您的反馈，我们会优化内容，给您最优质的体验' })
          this.show = false
        } catch (error) {
          Dialog.confirm({
            title: '温馨提示',
            message: '反馈失败，请登录'
          })
            .then(() => {
              // on confirm
              this.$router.push({
                path: '/login'
              })
            })
            .catch(() => {
              // on cancel
            })
          // Notify({ type: 'warning', message: '反馈失败-不许反馈！！！' })
          this.show = false
          this.actions = this.firstActions
        }
      }
    },
    // 点击返回按钮触发
    onCancel () {
      if (this.cancelText === '返回') {
        this.actions = this.firstActions
        this.show = true
        // 强制弹层不隐藏
        this.cancelText = '取消'
      } else {
        this.show = false
      }
    }
  }
}
</script>

<style scoped lang="less">
/* 标题样式 */
.title-box {
  display: flex;
  justify-content: space-between;
  align-items: flex-start;
  text-align: left;
}

/* label描述样式 */
.label-box {
  display: flex;
  justify-content: space-between;
  align-items: center;
}

/* 文章信息span */
.label-box span {
  margin: 0 3px;
  &:first-child {
    margin-left: 0;
  }
}

/* 图片的样式, 矩形黄金比例：0.618 */
.thumb {
  width: 113px;
  height: 70px;
  background-color: #f8f8f8;
  object-fit: cover;
}

/* 三图, 图片容器 */
.thumb-box {
  display: flex;
  justify-content: space-between;
}
</style>
